<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/4
 * Time: 15:55
 */
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Pagination\LengthAwarePaginator;

use Illuminate\Support\Facades\Request;
use DB;
use \Illuminate\Support\Facades\Input;
use App\ModelStatistic\StatisticCommon;

class AreaFormProduct extends Model{
    
    public static function paginate()
    {
        $cp_area = Request::get('cp_area');
        $perPage = Request::get('per_page', 20);
        $page = Request::get('page', 1);

        $start = ($page-1)*$perPage;

        $tableAccount = 'platsdk_order';
        $rw = \DB::connection('mysql_bgac')->table($tableAccount)->select('cp_product_id',DB::raw('sum(tb_recharge_price.deal) as sum_deal, sum(tb_recharge_price.dollar) as sum_dollar,sum(tb_recharge_price.rmb) as sum_rmb , sum(1) as ChargeNo'))
        ->leftJoin('recharge_price', 'platsdk_order.cp_product_id', '=', 'recharge_price.recharge_id');
        self::SourceCondition($rw, $cp_area);
        
        $rw->groupby('cp_product_id');
        StatisticCommon::SortAble($rw, 'ChargeNo', 'DESC');

        $total = $rw->get()->count();
        $rw = $rw->skip($start)->take($perPage);
        $data = $rw->get();
        $data = $data->toArray();
//        var_dump($data);exit();

        // $data = json_decode($data, true);
        // \Log::debug(__METHOD__, [self::GetRechargeAlias()]);
        // 加入多语言字段
        $alias_trans_list = \App\Models\Pay\Recharge::GetRechargeAlias();

        $recharge_price = \App\Models\Pay\Recharge::getAll();
        foreach ($data as &$obj) {
            $obj->prop_name = $alias_trans_list[$obj->cp_product_id ];

            $conf_one = $recharge_price[$obj->cp_product_id ]?:[];
            $obj->game_rmb = $conf_one['rmb'];
            $obj->game_lyto = $conf_one['dollar'];

            $obj->cp_rmb = round($obj->cp_rmb / 100, 2);
        }
        \Log::debug(__METHOD__, [ $obj]);
        $list = static::hydrate($data);

        $paginator = new LengthAwarePaginator($list, $total, $perPage);

        $paginator->setPath(url()->current());
//var_dump($paginator);exit();
        return $paginator;
    }

    //模糊查询
    public static function SourceCondition(&$rw, $area)
    {
        $cond = Request::all();
        $area_extend = \App\Models\Area::GetAreaExtendInfo($area);

        if ($cond['cp_product_id']){
            $rw->where("cp_product_id", '=', $cond["cp_product_id"]);
        }

        $cond["charge_ok"] = Input::get('charge_ok', 1);
        if ($cond["charge_ok"]){
            $rw->where("OrderSerial", "<>", "");
        }

        $cond["cp_area"] = Input::get('cp_area', 0);
        $cond["cp_area"] == env('CP_AREA_PASSWD') && $cond['cp_area'] = 0;
        if ($cond["cp_area"]){
            $rw->where("cp_area", "=", $cond['cp_area']);
        }

        $created_time = Request::get('created_time', []);
        // 默认仅仅查第一天的数据
        $start_time = StatisticCommon::GetIntTime($created_time['start'] ? : strtotime(date('Y-m-d',time())) );
        if ($start_time) {
            $rw->where('created_time', ">=",  $start_time);
        }

        $end_time = StatisticCommon::GetIntTime($created_time['end']);
        if ($end_time) {
            $rw->where('created_time', "<=", $end_time );
        }
        $deleted_no = $cond['deleted_at'] ?: 0;
        $rw->where('deleted_at','=', $deleted_no);
        \Log::debug('order list area created_time', [$rw->toSql(), $start_time, $end_time]);
    }
}